package com.example.boot.modules.marketing.controller;

import com.example.boot.modules.marketing.entity.Carousel;
import com.example.boot.modules.marketing.service.ICarouselService;
import com.example.boot.modules.marketing.vo.CarouselVo;
import com.example.starter.util.Response;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 轮播图配置
 * </p>
 *
 * @author 王令
 * @since 2024-02-27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/marketing/carousel")
public class CarouselController {

    private final ICarouselService service;

    @PostMapping
    public Response<Void> save(@RequestBody @Validated Carousel carousel) {
        service.saveOrUpdate(carousel);
        return Response.ok();
    }

    @DeleteMapping("/{id}")
    public Response<Void> removeById(@PathVariable Long id) {
        service.removeById(id);
        return Response.ok();
    }

    @GetMapping("/list")
    public Response<List<Carousel>> list() {
        List<Carousel> list = service.list();
        return Response.ok(list);
    }

    @GetMapping("/{id}")
    public Response<Carousel> getById(@PathVariable Long id) {
        Carousel carousel = service.getById(id);
        return Response.ok(carousel);
    }

    @GetMapping("/c/{code}")
    public Response<CarouselVo> getByCode(@PathVariable String code) {
        final CarouselVo carouselVo = service.getByCode(code);
        return Response.ok(carouselVo);
    }

}
